package cn.xuchunh.campusbbs.ui.fragments;

import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.base.BaseFragment;

/**
 * Created by XuChunH on 2015/10/17.
 */
public abstract class TabFragment extends BaseFragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private PagerAdapter adapter;

    private int tabLayoutId = R.id.tablayout;
    private int viewPagerId = R.id.viewpager;

    @Override
    protected void initView(View view) {
        viewPager = (ViewPager) view.findViewById(viewPagerId);
        viewPager.setAdapter(adapter);
        tabLayout = (TabLayout) view.findViewById(tabLayoutId);
        ViewCompat.setElevation(tabLayout, getResources().getDimensionPixelSize(R.dimen.elevation));
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void beforeInitView() {
        setContentViewRes(R.layout.layout_tab);
        setAdapter(createAdapter());
    }

    protected abstract PagerAdapter createAdapter();

    public void setViewPagerId(int viewPagerId) {
        this.viewPagerId = viewPagerId;
    }

    public void setTabLayoutId(int tabLayoutId) {

        this.tabLayoutId = tabLayoutId;
    }

    public TabLayout getTabLayout() {
        return tabLayout;
    }

    public ViewPager getViewPager() {
        return viewPager;
    }

    public PagerAdapter getAdapter() {
        return adapter;
    }

    public void setAdapter(PagerAdapter adapter) {
        this.adapter = adapter;
    }
}
